
<template>
	<view class="content" >
		<view class="navigation" ref="navigationRef">
			<eui-nav-bar title="功能列表" rightIcon="none" color="#000000" backgroundColor="#FFFFFF"/>
			<view class="sticky">
				<eui-search-bar 
					class="uni-mt-10 " 
					radius="20" 
					placeholder="请输入功能名称" 
					clearButton="none" 
					@confirm="screenOperation" 
					bgColor="#F9F9F8"
					v-model="c_seachText"
				>
					<template #searchFields>
						<eui-button title="dsafsadf"></eui-button>
					</template>
				</eui-search-bar>
			</view>
		</view>

		<scroll-view class="scroll-view" scroll-y :style="{height: scrollHeight + 'px'}" @refresherrefresh="queryOperation" >
			<eui-card v-if="selectDataArray.length > 0">
				<eui-virtual-list :origList="selectDataArray"  :options="options" type="list3" @itemClick="determine"/>
			</eui-card>
		</scroll-view>
	</view>
</template>



<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import { ref, onMounted, nextTick } from 'vue';
import service from '@/common/service';
import { useMainStore } from '../../../store/modules/store';
const mainStore = useMainStore();
const { loginInfo } = mainStore;

import { useLoginStore } from '@/store/modules/useLoginStore';
const LoginStore = useLoginStore();

import { useEventBus } from '@/store/useEventBus';
const EventBus = useEventBus();

const IsMult = ref<string>('');
const Menus = ref([]);
const c_where = ref<string>('');
const Tag = ref<string>('');
const MenuExtendCode = ref<string>('');
onLoad((option) => {
	if (option && option.extras) {
		const extras = JSON.parse(option.extras);
		IsMult.value = extras.IsMult;
		c_where.value = extras.ExtendData.c_where;
		MenuExtendCode.value = extras.ExtendData.MenuExtendCode;
		Menus.value = extras.Menus;
		Tag.value = extras.Tag == undefined ? '' : extras.Tag;
	}
	queryOperation();
});


/* 初始化高度*/
const scrollHeight = ref<number>(0);

onMounted(async () => {
	await nextTick();
	uni.getSystemInfo({
		success: (res) => {
			const h = res.windowHeight - res.safeAreaInsets.bottom;
			uni.createSelectorQuery().select('.navigation').boundingClientRect(data => {
				if (data) {
					// 滚动区域上下margin边距: 20;
					scrollHeight.value = h - data.height - 20;
				}
			}).exec();
		}
	})
})


/*列表相关内容*/
interface MenuObject {
    c_menu_code: string;
    c_mdcode: string;
    c_title: string;
    c_menu_note: string;
    c_menu_img?: string; 
    c_start_color?: string;
    c_end_color?: string;
	c_id?: string | number;
	name?: string;
}
const dataArray = ref<MenuObject[]>([]);
const menuData = ref<MenuObject[]>([]);
const selectDataArray = ref<MenuObject[]>([]);
	const options = ref({
    leftParam: 'c_title',
});
// 查询功能列表
const queryOperation = () => {
	service.enjoyShowLoading('正在查询...');
	let request = {
		url: LoginStore.EnjoyMain,
		data: {
			UniqueKey: "管易通40公共",
			ObjectData: loginInfo.sUser,
			MethodName: "GetMobileAllMenu",
		},
	};
	dataArray.value = [];
	menuData.value = [];
	service.request(request, (res: any) => {
		if (res && res.data && !res.data.HasException) {
			const objectData = res.data.ObjectData;
			if (objectData != null && objectData.length > 0) {
				menuData.value = objectData.filter(item => item.c_md_code).map(item => ({
					c_menu_code: item.c_menu_code,
					c_mdcode: item.c_md_code, // Functional code
					c_title: item.c_menu_title, // Menu name
					c_menu_note: item.c_system, // System to which it belongs
					c_menu_img: item.c_menu_img, // Menu icon
					c_start_color: item.c_start_color,
					c_end_color: item.c_end_color,
				}));

				dataArray.value = selectDataArray.value = menuData.value.map((obj, index) => ({
					c_id: index,
					...obj,
				}));
			} else {
				uni.showToast({  duration:3500,
					icon: 'none',
					title: res.data.Exception.Message
				});
			}
		}
		service.enjoyHideLoading();
	}, () => {
		service.enjoyHideLoading();
		console.log('失败')
	});
};


/* 筛选功能名称 */
type EventObject = {
    value: string;
};
const c_seachText = ref<string>('');
const screenOperation = (evt: EventObject) => {
	console.log('回车了', evt.value);
	if (evt.value != '') {
		selectDataArray.value = [];
		selectDataArray.value = dataArray.value.filter(item => item.c_title.includes(evt.value))
	} else {
		selectDataArray.value = dataArray.value;
	}
};

// 点击确认
type ItemObject = {
    index: number;
	item: MenuObject
};
const determine = (data: ItemObject) => {
	console.log(175, data.item.c_title);
	if (c_where.value == 'home') {
		let isExistsCurMenu = Menus.value.some(item => item.c_title == data.item.c_title)
		
		if (isExistsCurMenu) {
			uni.showToast({  duration:3500,
				icon: 'none',
				title: '当前模块已存在，请重新选择！'
			});
			return false;
		}
		EventBus.toggleHomeMenu(true);
		AddOftenMenuByUser(data.item)
	}
};

const AddOftenMenuByUser = (value: MenuObject) => {

	service.enjoyShowLoading('正在保存...');
	let request = {
		url: LoginStore.EnjoyMain,
		data: {
			UniqueKey: "管易通40公共",
			ObjectData: {
				c_userno: loginInfo.sUser ? loginInfo.sUser : '0006', // 用于苹果审核特殊处理获取不到用户时默认按0006账号获取this.loginInfo.sUser
				c_menu_extend: '管易通用户常用功能',
				c_mdcode: value.c_mdcode,
				c_type: '正常模块',
				c_title: value.c_title,
				c_menu_img: value.c_menu_img,
				c_menu_note: value.c_menu_note,
      		},
			MethodName: "AddOftenMenuByUser",
		},
	};

	service.request(request, (res: any) => {
		if (res && res.data && !res.data.HasException) {
			const objectData = res.data.ObjectData;
			if (objectData) {
				uni.navigateBack({
					delta: 1
				});
			} else {
				uni.showToast({  
					duration:3500,
					icon: 'none',
					title: res.data.Exception.Message
				});
			}

		} else {
			uni.showToast({
				duration: 3500,
				icon: 'none',
				title: res.data.Exception.Message || '',
				mask: true,
			});
		}
		service.enjoyHideLoading();
	}, () => {
		service.enjoyHideLoading();
		console.log('失败')
	});
}
</script>

<style scoped lang="scss">
	.navigation {
		background: #fff;
	}
</style>
